package cn.pzhuweb.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Yancy xiao
 * @create 2022-11-15 8:36
 */
public class C3P0Util {
    //1.声明数据源
    private static DataSource ds = null;

    //2.初始化
    static {
        ds = new ComboPooledDataSource();
        //ds=new ComboPooledDataSource("otherc3p0")
    }

    //3.提供共有方法得到数据源
    public static DataSource getDs() {
        return ds;
    }

    //4.提供公共方法得到连接
    public static Connection getConnection() {
        try {
            return ds.getConnection();//从连接池中得到连接
        } catch (SQLException e) {
            e.printStackTrace();//连接池没有可用连接
            return null;
        }

    }

    @Test
    public void test() throws SQLException {
        for (int i = 0; i < 10; i++) {
            System.out.println(getDs().getConnection());
        }
    }
}
